home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Simulation / PDP-8 Simulator / Source Code / Editor2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-13  |  4.5 KB  |  199 lines  |  [TEXT/KAHL]

  1. /*************************************************************************************
  2. *
  3. *        Editor2.c    Defines code for editing within text editor
  4. *
  5. *        Modification History:
  6. *        20/2/92 created from scratch.    
  7. *
  8. *
  9. *
  10. *************************************************************************************/
  11. #include    "EditGlobalEqu.p"
  12.  
  13. #include "Editor2.proto.h"
  14.  
  15. textEdHdl     GetWEditRecord(WindowPtr theWindow);
  16. lineHdl     GetListHead(textEdHdl theEditRec);
  17. lineHdl     GetLinkHandle(lineHdl listHead,int linkID);
  18. lineHdl     NewEmptyLine(int nChars);
  19. lineHdl     GetListTail(textEdHdl theEditRec);
  20. pascal void  ScrollProc(ControlHandle theControl,int partCode);
  21. char    IsEditKind(WindowPtr theWindow);
  22.  
  23.  
  24. TEHandle  GetEditRec(textEdHdl theText)
  25. {
  26.     /* returns lineEditRec field of text record, which is a textEdit handle */
  27.     if (theText != NIL)
  28.         return((*theText)->lineEditRec);
  29.     else
  30.         return(NIL);
  31. }
  32.  
  33.  
  34. textEdHdl GetMasterERecord(TEHandle tRec)
  35. {
  36.     /* reverse of above- given a text edit record, returns handle of master structure */
  37.     
  38.     GrafPtr     owner;
  39.     
  40.     if (tRec != NIL) {
  41.         owner = (*tRec)->inPort;
  42.         if (IsEditKind(owner))
  43.             return(GetWEditRecord(owner));
  44.     }
  45.     return(NIL);
  46. }
  47.  
  48.  
  49. InsertLink(lineHdl theLink,lineHdl *head,lineHdl *tail,int afterLink)
  50. {
  51.     /* inserts new link into chain, after the link with ID afterLink. If afterLink is
  52.         0, then the link becomes the first link. */
  53.     
  54.     lineHdl        next,last,after;
  55.     
  56.     after = GetLinkHandle(*head,afterLink);    
  57.     if (theLink != NIL) {
  58.         (*theLink)->lastLine = after;
  59.         if (after != NIL) {
  60.             next = (*after)->nextLine;
  61.             (*after)->nextLine = theLink;
  62.             if (next == NIL)
  63.                 *tail = theLink;
  64.         }
  65.         else {
  66.             next = *head;
  67.             *head = theLink;
  68.         }
  69.         (*theLink)->nextLine = next;
  70.         if (next != NIL)
  71.             (*next)->lastLine = theLink;    
  72.     }        
  73. }
  74.  
  75.  
  76. int ReplaceLink(lineHdl newLink,lineHdl *head,lineHdl *tail,int oldLink)
  77. {
  78.     /* replaces the link with ID = oldLink with the new link. The old one is disposed
  79.         of. If the old link does not exist, this routine does nothing & returns FALSE */
  80.     
  81.     lineHdl    oldLk,next,last;
  82.     
  83.     oldLk = GetLinkHandle(*head,oldLink);
  84.     if (oldLk != NIL) {
  85.         next = (*oldLk)->nextLine;
  86.         last = (*oldLk)->lastLine;
  87.         (*newLink)->nextLine = next;
  88.         (*newLink)->lastLine = last;
  89.                     
  90.         if (next != NIL)
  91.             (*next)->lastLine = newLink;
  92.         else
  93.             *tail = newLink;
  94.             
  95.         if (last != NIL)
  96.             (*last)->nextLine = newLink;
  97.         else
  98.             *head = newLink;
  99.  
  100.         DisposHandle(oldLk);
  101.         return(TRUE);
  102.     }
  103.     return(FALSE);
  104. }
  105.  
  106.  
  107. DeleteLink(lineHdl theLink,lineHdl *head,lineHdl *tail)
  108. {
  109.     /* deletes link from list, patching as required and modifying head and tail if rqd */
  110.     
  111.     lineHdl        next,last;
  112.     
  113.     if (theLink != NIL) {
  114.         next = (*theLink)->nextLine;
  115.         last = (*theLink)->lastLine;
  116.         
  117.         if (next != NIL)
  118.             (*next)->lastLine = last;
  119.         else
  120.             *tail = last;
  121.             
  122.         if (last != NIL)
  123.             (*last)->nextLine = next;
  124.         else
  125.             *head = next;
  126.             
  127.         DisposHandle(theLink);
  128.     }
  129. }
  130.  
  131.  
  132. lineHdl    MakeLink(TEHandle edRecord,int cCount)
  133. {
  134.     /* given a text edit record, a link is created of the right size, and the text is
  135.         copied into it. Then the handle is returned for your delectation & delight */
  136.         
  137.     lineHdl        theLink = NIL;
  138.     Handle        eText;
  139.     
  140.     if (edRecord != NIL) {
  141.         if (cCount > 255)
  142.             cCount = 255;
  143.         theLink = NewEmptyLine(cCount);
  144.         if (theLink != NIL) {
  145.             eText = (*edRecord)->hText;
  146.             HLock(eText);
  147.             HLock((Handle) theLink);
  148.             BlockMove(*eText,&(*theLink)->theLine[1],cCount);
  149.             HUnlock((Handle) theLink);
  150.             HUnlock(eText);
  151.             (*theLink)->theLine[0] = cCount;
  152.         }
  153.     }
  154.     return(theLink);
  155. }
  156.  
  157.  
  158. ClickText(WindowPtr theWindow,Point mClick)
  159. {
  160.     /* when mouse is clicked in text, this function is called to recalculate the selection
  161.         of text, etc. mClick is in local coordinates */
  162.     
  163.     textEdHdl    theText;
  164.     Rect        pr,tView;
  165.     int            line,chLoc,chCnt;
  166.     int            margin;
  167.     TEHandle    eTxt;
  168.         
  169.     if (IsEditKind(theWindow)) {
  170.         theText = GetWEditRecord(theWindow);
  171.         if (theText != NIL) {
  172.              GetClipRect(theWindow,&pr);
  173.              
  174.              if (PtInRect(mClick,&pr)) {
  175.                  /* somewhere in text area, so lets get calculating! We need to know
  176.                      the line clicked, and the character in the line */
  177.                  
  178.                  eTxt = GetEditRec(theText);
  179.                  tView = (*eTxt)->viewRect;
  180.                  if (PtInRect(mClick,&tView)) {
  181.                      /* were clicking the line being edited, so pass click to TE */
  182.  
  183.                  }
  184.                  else {
  185.                      /* were moving to another line */
  186.                      margin = GetMarginOffset(theText);
  187.                      chLoc = (mClick.h+1 - margin)/(*theText)->mSpace;
  188.                      line = ((mClick.v - pr.top)/(*theText)->lineHeight) + (*theText)->topLine;
  189.                      
  190.                      /* only move it if the line exists! */
  191.  
  192.                  }
  193.              }
  194.         }
  195.     }
  196. }
  197.  
  198.  
  199.